package jay.home.bet.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import jay.home.bet.dao.OddsDao;
import jay.home.bet.domain.Comp;
import jay.home.bet.domain.Event;
import jay.home.bet.domain.League;
import jay.home.bet.domain.Team;
import jay.home.bet.misc.Util;

public class SqlMapOddsDao extends SqlMapClientDaoSupport implements OddsDao
{

	public Comp getComp(String id) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		return (Comp)getSqlMapClientTemplate().queryForObject("getComp", map);
	}
	
	/**home,road表示球队的id, time格式为"yyyy-MM-dd HH:mm:ss"*/
	public Comp getCompByDetail(String home, String road, String time) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("homeid", home);
		map.put("roadid", road);
		map.put("time", time);
		List<Comp> comps = (List<Comp>)getSqlMapClientTemplate().queryForList("getComp", map);
		if(comps == null || comps.size() == 0)return null;
		return comps.get(0);
	}

	public Event getEvent(String eventid) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", eventid);
		return (Event)getSqlMapClientTemplate().queryForObject("getEvent",map);
	}

	public List<Event> getEvents(int page, int range) throws DataAccessException
	{
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", null, (page - 1) * range, range);
	}
	
	/**所有未封盘的赛事, 包括正在接受投注的, 暂停的,未发布的*/
	public List<Event> getAliveEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	/**正在接受投注的*/
	public List<Event> getRunningEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		map.put("frozen", false);
		map.put("published", true);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	/**暂停的*/
	public List<Event> getSleepingEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		map.put("frozen", true);
		map.put("published", true);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);

	}
	
	/**早盘*/
	public List<Event> getMorningEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("fromtime", Util.tomorrow());
		return (List<Event>)getSqlMapClientTemplate().queryForList("getMorningEvents", map, (page - 1) * range, range);
	}
	
	/* 尚未发布*/
	public List<Event> getNoRunningEvents(int page, int range) throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		map.put("published", false);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}

	
	/**未结算的, 已经封盘*/
	public List<Event> getUnsettledEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", false);
		map.put("fromtime", Util.tomorrow());
		return (List<Event>)getSqlMapClientTemplate().queryForList("getUnsettledEvents", map, (page - 1) * range, range);
	}

	/**根据类别获取, type的值为"单式"或"滚球"*/
	public List<Event> getEventsByType(String type, int page, int range) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", type);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	public List<Event> getGunQiuEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("event_type", "滚球");
		map.put("published", true);
		map.put("frozen", false);
		map.put("alive", true);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	public List<Event> getDanshiEvents(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		map.put("published", true);
		map.put("frozen", false);
		map.put("event_type", "单式");
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	/**通过compid获取它对应的一列比赛中的一个*/
	public Event getEventByComp(String compid) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("compid", compid);
		List<Event> events = (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map);
		if(events == null || events.size() == 0)return null;
		return events.get(0);
	}
	
	/**通过compid获取它对应的一列比赛*/
	public List<Event> getEventsByComp(String compid, int page, int range) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("compid", compid);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}
	
	public List<Event> getEvents(Map map, int page, int range)throws DataAccessException
	{
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);
	}

	public League getLeague(String id) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		return (League)getSqlMapClientTemplate().queryForObject("getLeague", map);
	}

	public Team getTeam(String teamid) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", teamid);
		return (Team) getSqlMapClientTemplate().queryForObject("getTeam", map);
	}

	public void insertComp(Comp comp) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertComp", comp);
	}

	public void insertEvent(Event event) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertEvent", event);
	}

	public void insertLeague(League league) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertLeague", league);
	}

	public void insertTeam(Team team) throws DataAccessException
	{
		getSqlMapClientTemplate().insert("insertTeam", team);
	}

	public void updateComp(Comp comp) throws DataAccessException
	{
		getSqlMapClientTemplate().update("updateComp", comp);
	}

	public void updateEvent(Event event) throws DataAccessException
	{
		getSqlMapClientTemplate().update("updateEvent", event);
	}
	
	public void updateEventsByComp(Event event) throws DataAccessException
	{
		getSqlMapClientTemplate().update("updateEventsByComp", event);
	}

	public void updateLeague(League league) throws DataAccessException
	{
		getSqlMapClientTemplate().update("updateLeague", league);
	}

	public void updateTeam(Team team) throws DataAccessException
	{
		getSqlMapClientTemplate().update("updateTeam", team);
	}
	
	public List<Team> getTeamsByLeague( String leagueid ) throws DataAccessException
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("leagueid", leagueid);
		return (List<Team>)getSqlMapClientTemplate().queryForList("getTeam", map);
	}
  
	public List<League> getLeagues() throws DataAccessException
	{
		return (List<League>)getSqlMapClientTemplate().queryForList("getLeague", null);
	}
	
	public int getByEvent(String compId) throws DataAccessException
	{
		HashMap map = new HashMap();
		map.put("compid", compId);
		map.put("type", "单式");
		List<Event> list = getSqlMapClientTemplate().queryForList("getEvent", map);
		return list.size();
	}

	public void deleteEvent(String eventid) throws DataAccessException
	{
		HashMap map = new HashMap();
		map.put("eventid", eventid);
		getSqlMapClientTemplate().delete("deleteEvent", map);
	}
	
	public List<Event> getEventsUniqueComp(int page, int range)throws DataAccessException
	{
		Map map = new HashMap();
		map.put("alive", true);
		map.put("event_type", "单式");
		map.put("uniqueComp", true);
		return (List<Event>)getSqlMapClientTemplate().queryForList("getEvent", map, (page - 1) * range, range);

	}

	public List<Comp> getSettlableComps(int page, int range) throws DataAccessException
	{
		return (List<Comp>)getSqlMapClientTemplate().queryForList("getSettlableComps", null, (page - 1) * range, range);
	}

	public int countEvents(Map map)
	{
		return (Integer)getSqlMapClientTemplate().queryForObject("countEvents", map);
	}
}